<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class c_login_class extends AWS_CONTROLLER
{
	function setup()
	{
		admin_session_class::check_admin_login(array(), $this->user_id);
	}

	public function index_action()
	{
		$this->login_action();
	}

	public function login_action()
	{
		if(!$this->user_info['permission']['is_administortar'])
		{
			H::js_pop_msg('你没有访问权限', '/');
		}
		
		if (admin_session_class::get_admin_uid())
		{
			HTTP::redirect("/admin/main/");
		}
		
		if ($this->user_id)
		{
			TPL::assign('user_info', $this->user_info);
		}
		
		TPL::assign("url", base64_decode($_POST['url']));
		
		TPL::output("admin/login");
	}

	/**
	 * 登录处理
	 */
	public function login_process_ajax_action()
	{
		define('IN_AJAX', TRUE);
		
		if ((get_setting('admin_login_seccode') == 'Y') && !core_captcha::validate($_POST['seccode_verify'], false))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'seccode_verify'
			), "-1", "请填写正确的验证码"));
		}
		
		$url = urldecode(trim($_POST["fromurl"]));
		
		$user_name = FORMAT::safe(trim($_POST["username"]));
		
		$password = FORMAT::safe($_POST["password"]);
		
		if (get_setting('ucenter_enabled') == 'Y')
		{
			if (!$user_info = $this->model('ucenter')->login($user_name, $password))
			{
				$user_info = $this->model('account')->check_login($user_name, $password);			
			}
		}
		else
		{
			$user_info = $this->model('account')->check_login($user_name, $password);
		}
		
		if(!$this->user_info['permission']['is_administortar'])
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "你没有访问权限"));
		}
		
		if($user_info)
		{
			$this->model('account')->admin_logout();
				
			$this->model('account')->set_admin_login($user_info["uid"]);
				
			if (empty($url) || $url == 'undefined')
			{
				$url = get_js_url('/admin/main/');
			}
				
			H::ajax_json_output(AWS_APP::RSM(array(
				'url' => $url
			), "1", "登录成功"));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "帐号或密码错误。"));
		}
	}

	/**
	 * 退出
	 */
	function logout_action($return_url = "")
	{
		$this->model('account')->admin_logout();
		
		if ($return_url == "")
		{
			HTTP::redirect("/");
		}
		else
		{
			HTTP::redirect($return_url);
		}
	}
}